package com.anbinh.doctor.fragment.clinic;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.anbinh.doctor.DetailtActivity;
import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.DoctorManagerAdapter;
import com.anbinh.doctor.model.ResponseDoctor;
import com.anbinh.doctor.model.ResponseDoctor.Data.Doctors;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentDoctorList extends SherlockFragment {
	private static FragmentDoctorList fragment = null;
	private View mView;
	private CircleProgressBar loadding;
	private DoctorManagerAdapter adapter;
	private ListView mGridView;
	protected ArrayList<Doctors> mList;
	private Doctors currentDoctor;

	public static FragmentDoctorList newInstance() {
		if (fragment == null) {
			fragment = new FragmentDoctorList();
		}
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Bundle bundle = new Bundle();
		bundle.putInt(Utils.KEY, R.id.them_bac_si);
		bundle.putString(Utils.TITLE,
				getResources().getString(R.string.them_bac_si));
		bundle.putBoolean(Utils.CUSTOMBAR, false);
		bundle.putInt(Utils.REQUEST_CODE, Utils.ADD_LOCATION);
		Utils.getNewIntent(getActivity(), bundle);

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,
			com.actionbarsherlock.view.MenuInflater inflater) {
		menu.add(0, 1, Menu.NONE, "Thêm").setIcon(R.drawable.ic_add)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public void onResume() {
		super.onResume();
		((DetailtActivity) getActivity()).setActionBarTitle(getResources()
				.getString(R.string.them_bac_si));

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_message, null);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Utils.showMessage(getActivity(), "Thêm bác sĩ");
		mGridView = (ListView) mView.findViewById(R.id.listView1);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				currentDoctor = mList.get(position);
				Bundle bundle = new Bundle();
				bundle.putInt(Utils.KEY, Utils.DOCTOR_DETAILT);
				bundle.putBoolean(Utils.CUSTOMBAR, false);
				bundle.putString(Utils.TITLE,
						getResources().getString(R.string.chi_tiet_bac_si));
				Utils.getNewIntent(getActivity(), bundle);
			}

		});

		loadding = (CircleProgressBar) mView.findViewById(R.id.loadding);
		onLoadData();
	}

	public Doctors getCurrentDoctors() {
		return currentDoctor;
	}

	public void onLoadData() {
		loadding.setVisibility(View.VISIBLE);
		final UserLogin user = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		String token = user.getData().getToken();

		ServerController.getInstance(getActivity()).reuquestListDoctor(token,
				new OnRequest<ResponseDoctor>() {

					@Override
					public void onSuccess(ResponseDoctor data) {
						loadding.setVisibility(View.INVISIBLE);
						if (getActivity() == null)
							return;
						if (data.getData().getDoctors() == null
								|| data.getData().getDoctors().size() < 0)
							return;
						mList = data.getData().getDoctors();
						adapter = new DoctorManagerAdapter(getActivity(),
								R.layout.item_doctor, mList);
						mGridView.setAdapter(adapter);

					}

					@Override
					public void onError(int errorCode, String message) {

					}
				});
	}
}
